export default {
  init(cb) {
    // 发送请求有两种 fetch xhr
    const xhr = window.XMLHttpRequest;
    const oldOpen = xhr.prototype.open;
    xhr.prototype.open = function (method, url, async, username, password) {
      this.info = {
        method,
        url,
        async,
        username,
        password,
      };
      return oldOpen.apply(this, arguments);
    };
    const oldSend = xhr.prototype.send;
    xhr.prototype.send = function (value) {
      if (this.info) {
        this.info.startTime = Date.now();
        const fn = (type) => () => {
          this.info.endTime = Date.now();
          this.info.time = Date.now() - this.info.startTime;
          this.info.requesestSize = value ? value.length : 0;
          this.info.responseSize = this.responseText.length;
          this.info.type = type;
          cb(this.info);
        };
        this.addEventListener("load", fn("load"), false);
        this.addEventListener("error", fn("error"), false);
        this.addEventListener("abort", fn("abort"), false);
      }
      return oldSend.apply(this, arguments);
    };
    //window.fetch
  },
};
